package util;

import com.aspose.words.Document;
import com.aspose.words.SaveFormat;

import java.io.*;

/**
 * Pdf 工具类
 *
 * @date 2022/5/10
 */
public class PdfUtil {

    /**
     * word转pdf
     *
     * @param path word文件地址
     * */
    public static void word2pdfByPath(String path){
        try {
            File word = new File(path);
            if(word.exists()){
                word2pdfByFile(word);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * word转pdf
     *
     * @param word word文件
     * */
    public static void word2pdfByFile(File word){
        try {
            InputStream is = new FileInputStream(word);
            Document doc = new Document(is);
            String absolutePath = word.getAbsolutePath();
            int i = absolutePath.lastIndexOf(".");
            String pdfPath = absolutePath.substring(0, i) + ".pdf";
            FileOutputStream os = new FileOutputStream(pdfPath);
            doc.save(os, SaveFormat.PDF);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
